package com.finance.system.configure.websecurity;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;

@Data
@Component("financeUserDetailServiceManager")
public class FinanceUserDetailServiceManager implements UserDetailsService {

    private List<UserDetailsService> detailsServices;

    @Autowired
    private UserDetailsService userNamePasswordDetailService;

    @Autowired
    private UserDetailsService mobileUserService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        for (UserDetailsService detailsService : detailsServices) {
            UserDetails userDetails = detailsService.loadUserByUsername(s);
            if(userDetails != null){
                return userDetails;
            }
        }
        return null;
    }

    @PostConstruct
    public void userDetailsService(){
        detailsServices = Arrays.asList(userNamePasswordDetailService, mobileUserService);
    }
}
